﻿#ifndef CANVAS_H
#define CANVAS_H

#include <QObject>
#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_2_1>
#include <QMouseEvent>
#include "toolbar.h"
#include <QOpenGLTexture>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>


class Canvas : public QOpenGLWidget, protected QOpenGLFunctions_2_1
{
public:
    Canvas(QWidget *parent = 0);

protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();

    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);

private:
    typedef struct tagLineParams {
        QVector<QPoint> points;
        int color;                  // RRGGBB
        int alpha;                  // 00:透明, FF:不透明
        int width;
    }LineParams;

    typedef struct tagImageParams {
        QPoint  point;
        QString strPath;
    }ImageParams;

private:
    void initTextures();
    void initShaders();
    void setPicPath(QString path);

    QPointF winPt2GlPt(QPoint winPt);   // 窗口坐标转换为OpenGL坐标
    void DrawLine(const LineParams& line);
    void DrawImage(const ImageParams& image);
    void DrawGeometry();

private:
    bool                        m_bMouseDown = false;
    LineParams                  m_newLine;
    QVector<LineParams>         m_lines;
    QVector<ImageParams>        m_images;

    Toolbar*                    m_pToolbar = nullptr;
    bool                        m_bTrans = false;           // 穿透
    bool                        m_bAnnotate = false;        // true ==> 批注， false ==> 白板
    int                         m_iOriginWidth;
    int                         m_iOriginHeight;
    bool                        m_bScaleDown =false;

    QVector<QVector3D> vertices;
    QVector<QVector2D> texCoords;
    QOpenGLShaderProgram program;
    QOpenGLTexture *texture;
    QMatrix4x4 projection;

    QString m_picPath;
    QImage m_img;
    bool  picChange = false;
};

#endif // CANVAS_H
